﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media.Imaging;
using System.IO;
namespace WPMind.WP7.Utils
{
    /// <summary>
    /// Helper class with some static methods
    /// </summary>
    public static class Helper
    {
        /// <summary>
        /// Save a writeable bitmap to media library
        /// </summary>
        /// <param name="bitmap">WriteableBitmap</param>
        /// <param name="name">Picture name</param>
        /// <param name="quality">Picture quality(1~100)</param>
        public static void SaveToMediaLibrary(this WriteableBitmap bitmap, string name, int quality)
        {
            if (quality < 1 || quality > 100)
            {
                throw new ArgumentOutOfRangeException("quality", "The quality should between 1 and 100");
            }
            using (var stream = new MemoryStream())
            {
                // Save the picture to the WP7 media library
                bitmap.SaveJpeg(
                   stream,
                   bitmap.PixelWidth,
                   bitmap.PixelHeight,
                   0,
                   quality);
                stream.Seek(0, SeekOrigin.Begin);
                new MediaLibrary().SavePicture(name, stream);
            }
        }
    }
}
